package cn.hexcloud.dga.governance.assessor.spec;

import cn.hexcloud.dga.common.constant.MetaConst;
import cn.hexcloud.dga.governance.assessor.Assessor;
import cn.hexcloud.dga.governance.bean.AssessParam;
import cn.hexcloud.dga.governance.bean.GovernanceAssessDetail;
import cn.hexcloud.dga.meta.bean.TableMetaInfo;
import cn.hexcloud.dga.meta.bean.TableMetaInfoExtra;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.regex.Pattern;

@Component("TABLE_NAME_STANDARD")
public class TableNameStandardAssessor extends Assessor {
    Pattern odsPattern = Pattern.compile("^ods_.+_(inc|full)$");
    Pattern dimPattern = Pattern.compile("^dim_.+_(zip|full)$");
    Pattern dwdPattern = Pattern.compile("^dwd_.+_(inc|full)$");
    Pattern dwsPattern = Pattern.compile("^dws_.+_(\\d+d|td|nd)$");
    Pattern adsPattern = Pattern.compile("^ads_.+");
    Pattern dmPattern = Pattern.compile("^dm_.+");

    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception {
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        String dwLevel = tableMetaInfo.getTableMetaInfoExtra().getDwLevel();
        String getGovernanceUrl = assessParam.getGovernanceMetric().getGovernanceUrl().replace("{tableId}", tableMetaInfo.getId() + "");
        if (dwLevel.equals("OTHER")) {
            governanceAssessDetail.setAssessScore(BigDecimal.valueOf(5));
            governanceAssessDetail.setAssessProblem("未纳入分层体系");
            governanceAssessDetail.setGovernanceUrl(getGovernanceUrl);
        } else if (dwLevel.equals("UNSET")) {
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("未设置分层，无法匹配规范");
            governanceAssessDetail.setGovernanceUrl(getGovernanceUrl);
        } else {
            Pattern pattern = null;
            switch (dwLevel) {
                case "ODS":
                    pattern = odsPattern;
                    break;
                case "DWD":
                    pattern = dwdPattern;
                    break;
                case "DWS":
                    pattern = dwsPattern;
                    break;
                case "DIM":
                    pattern = dimPattern;
                    break;
                case "ADS":
                    pattern = adsPattern;
                    break;
                case "DM":
                    pattern = dmPattern;
                    break;
                default:
            }
            if (!pattern.matcher(tableMetaInfo.getTableName()).matches()) {
                governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
                governanceAssessDetail.setAssessProblem("表名不符合规范");
                governanceAssessDetail.setGovernanceUrl(getGovernanceUrl);
            }
        }
    }
}
